package com.galaxy.order.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.order.domain.OrderPayment;
import com.galaxy.order.domain.bo.OrderPaymentBo;
import com.galaxy.order.domain.vo.OrderPaymentVo;
import com.galaxy.order.mapper.OrderPaymentMapper;
import com.galaxy.order.service.OrderPaymentService;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单支付信息Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class OrderPaymentServiceImpl extends ServiceImpl<OrderPaymentMapper, OrderPayment> implements OrderPaymentService {

    private final OrderPaymentMapper orderPaymentMapper;
    private final Converter converter;

    @DS("slave")
    @Override
    public TableDataInfo<OrderPaymentVo> queryPageList(OrderPaymentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderPayment> lqw = buildQueryWrapper(bo);
        var page = orderPaymentMapper.selectPage(pageQuery.build(), lqw);
        List<OrderPaymentVo> records = page.getRecords().stream()
            .map(orderPayment -> converter.convert(orderPayment, OrderPaymentVo.class))
            .collect(Collectors.toList());
        return new TableDataInfo<>(records, page.getTotal());
    }

    @DS("slave")
    @Override
    public List<OrderPaymentVo> queryList(OrderPaymentBo bo) {
        LambdaQueryWrapper<OrderPayment> lqw = buildQueryWrapper(bo);
        return orderPaymentMapper.selectList(lqw).stream()
            .map(orderPayment -> converter.convert(orderPayment, OrderPaymentVo.class))
            .collect(Collectors.toList());
    }

    @DS("slave")
    @Override
    public OrderPayment queryById(Long id) {
        return orderPaymentMapper.selectById(id);
    }

    @Override
    public Boolean insertByBo(OrderPaymentBo bo) {
        OrderPayment add = converter.convert(bo, OrderPayment.class);
        return orderPaymentMapper.insert(add) > 0;
    }

    @Override
    public Boolean updateByBo(OrderPaymentBo bo) {
        OrderPayment update = converter.convert(bo, OrderPayment.class);
        return orderPaymentMapper.updateById(update) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO: Add validation logic here
        }
        return orderPaymentMapper.deleteBatchIds(ids) > 0;
    }

    private LambdaQueryWrapper<OrderPayment> buildQueryWrapper(OrderPaymentBo bo) {
        LambdaQueryWrapper<OrderPayment> lqw = Wrappers.lambdaQuery();
        lqw.like(bo.getOrderNo() != null, OrderPayment::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getOrderId() != null, OrderPayment::getOrderId, bo.getOrderId());
        lqw.eq(bo.getPaymentType() != null, OrderPayment::getPaymentType, bo.getPaymentType());
        lqw.eq(bo.getStatus() != null, OrderPayment::getStatus, bo.getStatus());
        return lqw;
    }
}
